static int hub_interrupt_queued = 0;


	static void hub_complete_request (struct UsbbdDeviceRequest *req) { req->unused = NULL; DEBUG("HUB_OK\n"); }
	static void dev_complete_request (struct UsbbdDeviceRequest *req) { req->unused = NULL; DEBUG("DEV_OK\n"); }
	static void complete_request (struct UsbbdDeviceRequest *req) { req->unused = NULL; DEBUG("Interrupt OK\n"); }

	static void hub_interrupt_transmit (struct psfreedom_device *dev) {
		u8 data = 0;
		int i;
		
		//Prepare ports
		for (i = 0; i < 6; i++) {
			if (dev->hub_ports[i].change != 0)
			data |= 1 << (i+1);
		}
		
		if ( data != 0 ) {
			DEBUG("[!] Enviando byte de interrupcion (0x%X): ",data);
			if ( !hub_request.unused ) {
				hub_request.data = (const unsigned char *)&data; 
				hub_request.size = 1;
				hub_request.onComplete = &complete_request;
				hub_request.unused = &hub_request;
				hub_request.endpoint = &endpoints[1];
				hub_request.isControlRequest = 0;
				hub_request.next = NULL;
				hub_request.physicalAddress = NULL;     
				DEBUG("OK\n");
				sceUsbbdReqSend(&hub_request);
			}
			else {
				DEBUG("ERR\n");
			}
		}
		else { DEBUG("[!] Nada que reportar jefe!\n"); }
	}

	static void hub_port_changed (struct psfreedom_device *dev) {
		hub_interrupt_transmit (dev);
	}

	static void switch_to_port (struct psfreedom_device *dev, unsigned int port) {
		if (dev->current_port == port)
			return;
		
		//TODO : address de esto...
		
		DEBUG("Switching to port %d. Address is %d\n",port,dev->port_address[port]);
		
		dev->current_port = port;
		psfreedom_set_address (dev->gadget, dev->port_address[port]);
	}
	
	static void hub_connect_port (struct psfreedom_device *dev, unsigned int port) {
		if (port == 0 || port > 6)
			return;
		
		switch_to_port (dev, 0);

		/* Here, we must enable the port directly, otherwise we might loose time
			with the host asking for the status a few more times, and waiting for it to
			be enabled, etc.. and we might miss the 5seconds window in which we need
			to connect the JIG */
		dev->hub_ports[port-1].status |= PORT_STAT_POWER;
		dev->hub_ports[port-1].status |= PORT_STAT_CONNECTION;
		dev->hub_ports[port-1].status |= PORT_STAT_ENABLE;

		/* If the speed flag set is not the same as what the device suports, it will
			not work */
		
		//if (psfreedom_is_high_speed ())
		//	dev->hub_ports[port-1].status |= PORT_STAT_HIGH_SPEED;
		//else if (psfreedom_is_low_speed ())
		//	dev->hub_ports[port-1].status |= PORT_STAT_HIGH_SPEED;

		dev->hub_ports[port-1].change |= PORT_STAT_C_CONNECTION;
		
		hub_port_changed (dev);
	}
	
	static void hub_disconnect_port (struct psfreedom_device *dev, unsigned int port) {
		if (port == 0 || port > 6)
			return;
		switch_to_port (dev, 0);
		dev->hub_ports[port-1].status &= ~PORT_STAT_CONNECTION;
		dev->hub_ports[port-1].status &= ~PORT_STAT_ENABLE;
		dev->hub_ports[port-1].status &= ~PORT_STAT_HIGH_SPEED;
		dev->hub_ports[port-1].status &= ~PORT_STAT_LOW_SPEED;
		dev->hub_ports[port-1].change |= PORT_STAT_C_CONNECTION;
		hub_port_changed (dev);
	}
	
	// TODO: arreglar usb_ep_enable.
	static int set_hub_config(struct psfreedom_device *dev) {
		int err = 0;
		hub_interrupt_queued = 0;
		//err = usb_ep_enable(dev->hub_ep, &hub_endpoint_desc);
		if (err) {
		//	ERROR(dev, "can't start %s: %d\n", dev->hub_ep->name, err);
			goto fail;
		}
		//dev->hub_ep->driver_data = dev;
	
		fail:
			/* caller is responsible for cleanup on error */
		return err;
	}
	
	// TODO: arreglar usb_ep_disable
	static void hub_reset_config(struct psfreedom_device *dev) {
		//DBG(dev, "reset config\n");
		//usb_ep_disable(dev->hub_ep);
		hub_interrupt_queued = 0;
	}
	
	//TODO HUB ENDPOINTS
	static int hub_set_config(struct psfreedom_device *dev, unsigned number) {
		int result = 0;
		struct usb_gadget *gadget = dev->gadget;
		hub_reset_config(dev);
		result = set_hub_config(dev);
		//if (!result && !dev->hub_ep) { result = -ENODEV; }
		if (result) {
			hub_reset_config(dev);
		} else {
			char *speed;
			switch (gadget->speed) {
				case USB_SPEED_LOW:		speed = "low"; break;
				case USB_SPEED_FULL:	speed = "full"; break;
				case USB_SPEED_HIGH:	speed = "high"; break;
				default:		speed = "?"; break;
			}
			//INFO(dev, "%s speed\n", speed);
		}
		return result;
	}
	
	static void hub_disconnect (struct usb_gadget *gadget) {
		struct psfreedom_device *dev = get_gadget_data (gadget);
		hub_reset_config (dev);
	}